﻿using Microsoft.Extensions.DependencyInjection;
using ProcessBase.Models;
using SubstrateHelper.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SubstrateHelper.Helpers
{
    public class LotNoHelper
    {
        public static string GetMainLotNo(string lot)
        {
            using (var scope = Global.Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var Db = services.GetRequiredService<SubstrateHelperDbContext>();
                    return Db.ProcessStatistics.Where(x => x.LotNoOutput == lot).Select(x => x.MainLotNo).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    return null;
                }
            }            
        }

        public static Order GetOrderFromOutputLotNo(string lot)
        {
            using (var scope = Global.Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var Db = services.GetRequiredService<SubstrateHelperDbContext>();
                    var mainLot= Db.ProcessStatistics.Where(x => x.LotNoOutput == lot).Select(x => x.MainLotNo).FirstOrDefault();
                    if (mainLot != null)
                    {
                        return Db.Orders.Where(x => x.vbatchcode == mainLot).FirstOrDefault();
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
    }
}
